<?php

namespace app\admin\controller\system;

use app\Base;
use app\service\UploadServices;
use Exception;
use think\response\Json;

/**
 * 文件上传
 * User : Du
 * Date : 2022/12/2
 * Time : 13:29
 * Class Upload
 * @package projects\controller\admin
 */
class Upload extends Base
{

    /**
     * 获取七牛云上传token
     *
     * User : Du
     * Date : 2022/3/31
     * Time : 16:58
     * @param UploadServices $services
     * @return Json
     */
    public function token(UploadServices $services): Json
    {
        $res = $services::getToken();
        $this->res['data'] = $res;
        return $this->returnData();
    }

    /**
     * 上传图片
     * User : Du
     * Date : 2022/11/22
     * Time : 16:32
     * @param UploadServices $services
     * @return Json
     * @throws Exception
     */
    public function image(UploadServices $services): Json
    {
        $res = $services::image($this->params);
        if (is_string($res)) {
            return $this->returnData('fail', $res);
        }
        $this->res['data'] = $res;
        return $this->returnData();
    }

    /**
     * 上传视频
     *
     * User : Du
     * Date : 2022/3/31
     * Time : 14:31
     * @param UploadServices $services
     * @return Json
     * @throws Exception
     */
    public function video(UploadServices $services): Json
    {
        $res = $services::video($this->params);
        if (!$res) {
            return $this->returnData('fail', '上传失败');
        }
        $this->res['data'] = $res;
        return $this->returnData();
    }

    /**
     * 音乐上传
     * @param UploadServices $services
     * @return Json
     * @throws Exception
     */
    public function music(UploadServices $services): Json
    {
        $res = $services::music();
        if (!$res || is_string($res)) {
            return $this->returnData('fail', !$res ? '上传失败' : $res);
        }
        $this->res['data'] = $res;
        return $this->returnData();
    }

    /**
     * 删除文件
     * User : Du
     * Date : 2022/12/2
     * Time : 15:52
     * @param UploadServices $services
     * @return Json
     */
    public function delete(UploadServices $services): Json
    {
        $res = $services::delete($this->params);
        if (!$res) {
            return $this->returnData('deleteFail');
        }
        return $this->returnData();
    }
}